package com.cll.prototype.redis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2020/12/5 19:42
 */
@RestController
@RequestMapping(value = "/redis")
public class RedisController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping(value = "/setWithExpireTime")
    public String setWithExpireTime(String key, String value){
        redisTemplate.opsForValue().set(key, value, 10, TimeUnit.SECONDS);
        return "success";
    }

    @RequestMapping(value = "/get")
    public String get(String key){
        Object o = redisTemplate.opsForValue().get(key);
        if (null == o) {
            return "key not exists!";
        }
        return o.toString();
    }
}